<?php
#############################################################################
# Parse functions for http://etvnet.com site.                               #
#                                                                           #
# Author: consros 2010                                                      #
#############################################################################

require_once "pageTools.inc";
require_once "settings.inc";
require_once "tools.inc";
require_once "etvFunctions.inc";
require_once "parsers.inc";

function displayVideoStyles() {
?>
<style type="text/css">
    div.title      { font-size: 18pt; font-style: bold; color: #FFFFFF; }
    div.descr      { font-size: 12pt; }
    td             { font-size: 13pt; }
</style>
<?php
}

function displayDescription($html) {
    $descr = preg_replace('|.*?<div class="description">(.*?)</div>.*|', '$1', $html);
    $descr = preg_replace('|<a .*?>(.*?)</a>|', '$1', $descr);
    print "<div class=\"descr\">$descr</div>\n";
}

function displayInfo($html) {
    $info = preg_replace('|.*(<ul>.*?</ul>)</div><div class="screenshots">.*|', '$1', $html);
    $info = preg_replace('|<a .*?>(.*?)</a>|', '$1', $info);
    if (preg_match_all('|<li>(.+?)</li>|', $info, $lines)) {
        print "<table cellspacing=\"0\" cellpadding=\"0\">\n";
        foreach ($lines[1] as $line) {
            print "<tr><td>$line</td></tr>\n";
        }
        print "</table>\n";
    }
}

function displayScreenshots($html) {
    $screenshots = parseScreenshots($html);
    print "<div align=\"center\"><table><tr>\n";
    $i = 0;
    foreach ($screenshots as $thumb => $img) {
        if ($i++ == 5) {
            $i = 1;
            print "</tr><tr>\n";
        }
        print "<td><a href=\"$img\" vod>";
        print '<img src="' . $thumb . '" width="140" height="100" />';
        print "</a></td>\n";
    }
    print "</tr></table></div>\n";
}

function getStreamUrl($path, $quality) {
    $functions = new EtvFunctions(DEBUG);
    $url = $functions->getShortStreamUrl($path, $quality);
    if (EMBEDDED_BROWSER) {
        $url = preg_replace('|mms:|', 'rtsp:', $url);
    }
    return $url;
}

function displayVideoPage($html, $path) {

    $prebuf = BUFFER_SIZE > 0 ? ('prebuf="'.(BUFFER_SIZE * 1024).'"') : "";

    $linkLow = 'watch.php?path=' . urlencode($path) . '&q=1';
    $linkLow = "<a href=\"$linkLow\" vod $prebuf>" . LANG_WATCH_LQ . "</a>";
    if (hasHQvideo($html)) {
        $linkNormal  = 'watch.php?path=' . urlencode($path) . '&q=2';
        $linkNormal  = "<a href=\"$linkNormal\" vod $prebuf>";
        $linkNormal .= LANG_WATCH_NQ . "</a>";

        $url = getStreamUrl($path, 3);
        $linkHigh  = "<a href=\"$url\" name=\"DEFAULTREF\" vod $prebuf>";
        $linkHigh .= LANG_WATCH_HQ . "</a>";

        $links = "$linkHigh&nbsp;$linkNormal&nbsp;$linkLow";
    } else {
        $url = getStreamUrl($path, 2);
        $linkNormal  = "<a href=\"$url\" name=\"DEFAULTREF\" vod $prebuf>";
        $linkNormal .= LANG_WATCH . "</a>";

        $links = "$linkNormal&nbsp;$linkLow";
    }

    $title = preg_replace('|.*<title>(.*?)</title>.*|', '$1', $html);
    $title = preg_replace('| - eTVnet|', '$1', $title);
    print "<div class=\"title\">$title - $links</div><br />\n";

    # remember opened stream
    addToHistory("history/path.txt", $title, $path);

    displayDescription($html);
    print "<br />\n";
    displayInfo($html);
    # print "URL: $url<br />\n";
    print "<br />\n";
    displayScreenshots($html);
}

?>
